﻿var SCA = SCA || {};
SCA.UI = SCA.UI || {};
SCA.UI.ExplorerLocations = SCA.UI.ExplorerLocations || {};

SCA.UI.ExplorerLocations.Table = function () {

    this.AddRow = function (index) {
        var lastTr = jQuery('#TableExplorerLocations tr:last');
        var rowHtml = '<tr id="TRTableExplorerLocations' + index + '">\
                            <td>\
                                <input type="button" value="Edit" class="template-edit-button" onclick="OpenExplorerLocationsDialog(\'' + index + '\')"/>\
                                <input type="button" value="Delete" class="sbs-button" onclick="mainUI.explorerLocationsTable.DeleteRow(\'' + index + '\')"/>\
                            </td>\
                            <td id="TDTableExplorerLocations'+ index + '_1">\
                                ' + GetSiteSetting(SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations[index].SiteSettingID).Url +
                            '</td>\
                            <td id="TDTableExplorerLocations' + index + '_2">\
                                ' + SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations[index].ApplicationTypes.toString() + 
                            '</td>\
                      </tr>';
        lastTr.after(rowHtml);
    }

    this.DeleteRow = function (index) {
        jQuery("#TRTableExplorerLocations" + index).hide();
        SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations[index].IsDeleted = true;
    }

    var GetSiteSetting = function (siteSettingID) {
        var n = SCA.AppConfiguration.SiteSettings.length;

        for (var i = 0; i < n; i++) {
            var siteSetting = SCA.AppConfiguration.SiteSettings[i];
            if (siteSetting.ID == siteSettingID)
                return siteSetting;
        }

        console.error("SCA.UI.ExplorerLocations.Table - GetSiteSetting(): no site setting for id "+ siteSettingID);
        return null;
    }

    this.RefreshRow = function (index) {
        jQuery("#TDTableExplorerLocations" + index + "_1").html(GetSiteSetting(SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations[index].SiteSettingID).Url);
        jQuery("#TDTableExplorerLocations" + index + "_2").html(SCA.AppConfiguration.ExplorerConfiguration.ExplorerLocations[index].ApplicationTypes.toString());
    }
};